export function getLocation() {
  var options = {
    enableHighAccuracy: true,
    maximumAge: 1000,
  }
  if (!navigator.geolocation) {
    //浏览器不支持geolocation
    alert('浏览器不支持geolocation')
    return
  }

  if (window.location.protocol != 'https:') {
    // 使用ip定位

  } else {
    // 使用浏览器定位
    navigator.geolocation.getCurrentPosition(
      onSuccess,
      onError,
      options
    );
  }
}
function onSuccess(position) {
  let address = {
    latitude: position.coords.latitude,
    longitude: position.coords.longitude
  }
  window.localStorage.setItem('address', JSON.stringify(address))
}
function onError(error) {
  switch (error.code) {
    case 1:
      window.localStorage.setItem('address', '位置服务被拒绝')
      break

    case 2:
      window.localStorage.setItem('address', '暂时获取不到位置信息')
      break

    case 3:
      window.localStorage.setItem('address', '获取信息超时')
      break

    case 4:
      window.localStorage.setItem('address', '未知错误')
      break
  }
}